<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="药品名称" prop="drugstoreName">
        <el-input
          v-model="queryParams.drugstoreName"
          placeholder="请输入药品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['warehouse:drugstore:add']"
        >入库</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="drugstoreList" @selection-change="handleSelectionChange">
      <el-table type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="rugstoreId" />
      <el-table-column label="库房" align="center" prop="warehouseName" />
      <el-table-column label="名称" align="center" prop="drugstoreName" />
      <el-table-column label="数量" align="center" prop="drugstorenum" />
      <el-table-column label="供货商" align="center" prop="supplierName" />
      <el-table-column label="经办人" align="center" prop="skullName" />
      <el-table-column label="单位" align="center" prop="unitName" />
      <el-table-column label="批发价" align="center" prop="tradePrice" />
      <el-table-column label="售价" align="center" prop="sellingPrice" />
      <el-table-column label="产地" align="center" prop="areaName" />
      <el-table-column label="生产日期" align="center" prop="produceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.produceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效日期" align="center" prop="validDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="批号" align="center" prop="batch" />
      <el-table-column label="类型" align="center" prop="typeName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['warehouse:drugstore:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改药品库存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="drugstoreName">
          <el-input v-model="form.drugstoreName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="供货商" prop="supplierName">
          <el-select v-model="form.supplierId"  placeholder="请选择供货商">
            <el-option
               v-for="dict in suppOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="Number(dict.dictValue)"
                />
          </el-select>
        </el-form-item>
        <el-form-item label="经办人" prop="skullName">
          <el-select v-model="form.skullId"  placeholder="请选择经办人">
            <el-option
               v-for="dict in skullOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="Number(dict.dictValue)"
                />
          </el-select>
        </el-form-item>
        <el-form-item label="库房" prop="warehouseName">
          <el-input v-model="form.warehouseName" placeholder="请输入库房" />
        </el-form-item>
        <el-form-item label="单位" prop="unitName">
          <el-select v-model="form.unit"  placeholder="请选择单位">
            <el-option
               v-for="dict in unitOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="Number(dict.dictValue)"
                />
          </el-select>
        </el-form-item>
        <el-form-item label="批发价" prop="tradePrice">
          <el-input v-model="form.tradePrice" placeholder="请输入批发价" />
        </el-form-item>
        <el-form-item label="售价" prop="sellingPrice">
          <el-input v-model="form.sellingPrice" placeholder="请输入售价" />
        </el-form-item>
        <el-form-item label="产地" prop="areaName">
          <el-select v-model="form.area" placeholder="请选择产地">
            <el-option
               v-for="dict in areaOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="Number(dict.dictValue)"
                />
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="typeName">
            <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
               v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="Number(dict.dictValue)"
                />
          </el-select>
          </el-form-item>
        <el-form-item label="生产日期" prop="produceDate">
          <el-date-picker clearable
            v-model="form.produceDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效日期" prop="validDate">
          <el-date-picker clearable
            v-model="form.validDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择有效日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="数量" prop="drugstorenum">
          <el-input v-model="form.drugstorenum" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="批号" prop="batch">
          <el-input v-model="form.batch" placeholder="请输入批号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDrugstore, getDrugstore, delDrugstore, addDrugstore, updateDrugstore } from "@/api/warehouse/drugstore";

export default {
  name: "Drugstore",
  data() {
    return {
      unitOptions : [],
        areaOptions : [],
        typeOptions : [],
        suppOptions : [],
        skullOptions :[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 药品库存表格数据
      drugstoreList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        drugstoreName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        drugstoreName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        unitId: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        tradePrice: [
          { required: true, message: "批发价不能为空", trigger: "blur" }
        ],
        sellingPrice: [
          { required: true, message: "售价不能为空", trigger: "blur" }
        ],
        area: [
          { required: true, message: "产地不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        produceDate: [
          { required: true, message: "生产日期不能为空", trigger: "blur" }
        ],
        validDate: [
          { required: true, message: "有效日期不能为空", trigger: "blur" }
        ],
        drugstorenum: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
        batch: [
          { required: true, message: "批号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("database_drug_type").then(response => {
      this.unitOptions = response.data;
      });
      this.getDicts("database_drug_add").then(response => {
      this.areaOptions = response.data;
      });
      this.getDicts("database_drug_yao").then(response => {
      this.typeOptions = response.data;
      });
      this.getDicts("warehouse_drugstore_gong").then(response => {
      this.suppOptions = response.data;
      });
      this.getDicts("warehouse_drugstore_ren").then(response => {
      this.skullOptions = response.data;
      });
  },
  methods: {
    /** 查询药品库存列表 */
    getList() {
      this.loading = true;
      listDrugstore(this.queryParams).then(response => {
        this.drugstoreList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rugstoreId: null,
        drugstoreName: null,
        supplierId: null,
        skullId: null,
        warehouseId: null,
        unit: null,
        tradePrice: null,
        sellingPrice: null,
        area: null,
        type: null,
        produceDate: null,
        validDate: null,
        drugstorenum: null,
        batch: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rugstoreId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "入库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rugstoreId = row.rugstoreId || this.ids
      getDrugstore(rugstoreId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改药品库存";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rugstoreId != null) {
            updateDrugstore(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDrugstore(this.form).then(response => {
              this.$modal.msgSuccess("入库成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rugstoreIds = row.rugstoreId || this.ids;
      this.$modal.confirm('是否确认删除药品库存编号为"' + rugstoreIds + '"的数据项？').then(function() {
        return delDrugstore(rugstoreIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('warehouse/drugstore/export', {
        ...this.queryParams
      }, `drugstore_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
